#-*- coding:utf-8 -*-
#作用：学习测试文件
#作者：尚墨
#日期：2017年11月24日
#邮箱：ws1992jx@163.com

from urllib.request import urlopen
from urllib.error import HTTPError,URLError
from bs4 import BeautifulSoup

def getTitle(url):
	try:
		html=urlopen(url)
	except (HTTPError,URLError) as e:
		return None
	try:
		bsObj=BeautifulSoup(html.read())
		title=bsObj.body.h1
	except AttributeError as e:
		return None
	return title

urlid='http://www.pythonscraping.com/pages/page1'
title=getTitle(urlid)

if title == None:
	print ('Title could not be found')
else:
	print (title)